//==============================================================================
//
// Title:       ScreenRTC.c
// Purpose:     A short description of the implementation.
//
// Created on:  23/09/2010 at 08:37:36 by LEGRAND.
// Copyright:   GRPLEG. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files
#include "Screen_Init.h"
#include "Screen_Setup_1.h"
#include "Screen_EEPROM.h"
#include "led.h"
//==============================================================================
// Constants
//==============================================================================
// Types

//==============================================================================
// Static global variables

//==============================================================================
// Static functions
static unsigned char Screen_Init_OnInitialize( void );

static unsigned char Screen_Init_ProcessMessage( TMessage* pMsg );

//==============================================================================
// Global variables


//==============================================================================
// Global functions

const TScreen Screen_Init =
{
	.ProcessMessage 	= Screen_Init_ProcessMessage,
	.OnInitialize   	= Screen_Init_OnInitialize,
};

static unsigned char Screen_Init_OnInitialize( void )
{

  uInt16 vbat;

          LCD_Clear();

          vbat = Get_Voltage_On_The_Fly(BATTERY_1);

           LCD_DrawString("P1: ",1,LINE_2);
           if ((vbat >= MIN_VOLT_ABS ) AND (vbat <= MAX_VOLT_ABS))
           {
             LCD_DrawVoltage(vbat,5,LINE_2);
             SetFlag(FLAG_BATT1_PRESENT);
           }
           else
           {
             LCD_DrawString("Insert Batt ",5,LINE_2);
             ClearFlag(FLAG_BATT1_PRESENT);
           }


          vbat = Get_Voltage_On_The_Fly(BATTERY_2);

           LCD_DrawString("P2: ",1,LINE_1);

           if ((vbat >= MIN_VOLT_ABS ) AND (vbat <= MAX_VOLT_ABS))
           {
             LCD_DrawVoltage(vbat,5,LINE_1);
             SetFlag(FLAG_BATT2_PRESENT);
           }
           else{
             LCD_DrawString("Insert Batt ",5,LINE_1);
             ClearFlag(FLAG_BATT2_PRESENT);
           }

          SetTimerFine(TIMER_BLINK_ECRAN_INIT);

  return 0U;
}

static unsigned char Screen_Init_ProcessMessage( TMessage* pMsg )
{

  	if( pMsg->ID == MSG_KEY_BOARD AND (GetFlag(FLAG_BATT1_PRESENT) OR GetFlag(FLAG_BATT2_PRESENT)) )
	{

		switch( pMsg->Param1 )
		{
                                          case KEY_OK:
                                          {
                                            LCD_Clear();
                                            ClearFlag(FLAG_DUMMY1);
                                            ClearFlag(FLAG_DUMMY2);

                                            if( GetFlag(FLAG_BATT1_PRESENT)){
                                            Screen_SetCurrent(&Screen_Setup_1);
                                            }
                                            else if( GetFlag(FLAG_BATT2_PRESENT)){
                                            Screen_SetCurrent(&Screen_Setup_2);
                                            }
                                            else{
                                               __no_operation();
                                            }


                                          }
                                          break;
                                          case KEY_UP:
                                          {

                                          }
                                          break;
                                          case KEY_DOWN:
                                          {

                                          }
                                          break;
                                          case KEY_LEFT:
                                          {

                                          }
                                          break;
                                          case KEY_RIGHT:
                                          {

                                          }
                                          break;
                                }
                }
                 else if( pMsg->ID == MSG_KEY_BOARD AND !(GetFlag(FLAG_BATT1_PRESENT) OR GetFlag(FLAG_BATT2_PRESENT)) )
	{

                  if (pMsg->Param1 == KEY_OK)
                  {
                    Screen_SetCurrent(&Screen_EEPROM);
                  }

                }
                else if( pMsg->ID == MSG_TIMER )
                  {
                    if(GetFlag(FLAG_BATT1_PRESENT) OR GetFlag(FLAG_BATT2_PRESENT))
                    {
		switch( GetFlag(FLAG_DUMMY1) )
		{
                                case 0:
                                 LCD_DrawString("    Press OK    ",1,LINE_1);
                                 LCD_DrawString("    to Setup    ",1,LINE_2);
                                 SetFlag(FLAG_DUMMY1);                // sert a faire alterner le texte
                                 SetTimerFine(TIMER_BLINK_ECRAN_INIT);
                                 break;

                                case 1:
                                 ClearFlag(FLAG_DUMMY1);              // sert a faire alterner le texte
                                 Screen_SetCurrent(&Screen_Init);

                                break;

                                default :
                                  __no_operation();
                                break;

                                }
                    }
                    else
                    {
                               Screen_SetCurrent(&Screen_Init);
                    }

                  }
                  else
                  {

                   }



	return 0U;
}

